package com.ruoyi.reception;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.receipt.domain.TbExampleSubjectAnalysis;
import com.ruoyi.receipt.domain.TbExampleSubjectManager;
import com.ruoyi.receipt.domain.TbExampleSubjectRelation;
import com.ruoyi.receipt.domain.TbKnowledgeManager;
import com.ruoyi.receipt.service.*;
import com.ruoyi.receipt.utils.CompOutSubjectUtils;
import com.ruoyi.receipt.utils.CurrentUserUtils;
import com.ruoyi.receipt.utils.StatisAnalysisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/reception/emapleSubjectManager")
public class TbEmapleSubjectManagerRecController {
    @Autowired
    private CompOutSubjectUtils compOutSubjectUtils;
    @Autowired
    private ITbExampleSubjectManagerService tbExampleSubjectManagerService;
    @Autowired
    private ITbKnowledgeManagerService tbKnowledgeManagerService;
    @Autowired
    private ITbExampleSubjectRelationService tbExampleSubjectRelationService;
    @Autowired
    private StatisAnalysisUtils statisAnalysisUtils;
    @Autowired
    private ITbExampleSubjectOptionService tbExampleSubjectOptionService;
    @Autowired
    private ITbExampleSubjectAnalysisService tbExampleSubjectAnalysisService;

    @GetMapping("/managerlist")
    //知识点id
    public AjaxResult OutManager(@RequestBody Map knowledgeManagermap){
        Long userId= CurrentUserUtils.getCurrentUser().getId();
     //根据知识点查题簇
        Long knowledgeManagerId = Long.valueOf(String.valueOf(knowledgeManagermap.get("knowledgeManagerId")));
        List<Long> clusteId = tbExampleSubjectManagerService.selectClusteId(knowledgeManagerId);
        //查询例题个数
        TbKnowledgeManager tbKnowledgeManager = tbKnowledgeManagerService.selectTbKnowledgeManagerById(knowledgeManagerId);
        Integer exampleSubjectNumber = tbKnowledgeManager.getExampleSubjectNumber();
        List<Long> longs = compOutSubjectUtils.compOutSubject(userId, clusteId, exampleSubjectNumber);
        //需要根据题目id去查询题目和选项
        List list =new ArrayList();
        for (int i=0;i<exampleSubjectNumber;i++){
            Map map =new HashMap();
            TbExampleSubjectManager tbExampleSubjectManager = tbExampleSubjectManagerService.selectTbExampleSubjectManagerById(longs.get(i));
            List optionlist = tbExampleSubjectOptionService.selectoptionAll(longs.get(i));
            map.put("sunjectManagerId",tbExampleSubjectManager.getId());
            map.put("sunjectManagertitle",tbExampleSubjectManager.getTitle());
            map.put("optionlist",optionlist);
            list.add(map);
        }
        return  AjaxResult.success(list);
    }
    /**
     * 查询答案时推入
     */
    @PostMapping("/relaiton")
    public AjaxResult relation(@RequestBody Map map){
        Long userId= CurrentUserUtils.getCurrentUser().getId();
        //查询正确答案所对应的id
        Long subjectManagerId = Long.parseLong(String.valueOf(map.get("subjectManagerId")));
        String optiponId = map.get("optiponId")+"";
        List optiponIdList=new ArrayList();
        String[] optio=optiponId.split(",");
        optiponIdList = Arrays.asList(optio);
        List relationList = tbExampleSubjectRelationService.selectRelation(Long.valueOf(String.valueOf(subjectManagerId)));
        //推入所有题目id
        List managerList=new ArrayList<>();
        managerList.add(subjectManagerId);
        statisAnalysisUtils.putTodayAllSubject(userId,managerList);
        //比较数组长度
        if (relationList.size()==optiponIdList.size()){
            //正确
            Arrays.asList(optiponId).contains(relationList);
            statisAnalysisUtils.putTodayWrongSubject(userId,managerList);
        }else {
            statisAnalysisUtils.putTodayRightSubject(userId,managerList);
        }
        return AjaxResult.success(relationList);
    }

    //查看解析
    @PostMapping("/selectAnalysisOut")
    public AjaxResult selectAnalysisOut(@RequestBody Map map){
        Long subjectManagerId = Long.parseLong(String.valueOf(map.get("subjectManagerId")));
        TbExampleSubjectAnalysis subjectAnalysis=new TbExampleSubjectAnalysis();
        subjectAnalysis.setSubjectManagerId(subjectManagerId);
        List Analysislist = tbExampleSubjectAnalysisService.selectAnalysisOut(subjectAnalysis);
        return  AjaxResult.success(Analysislist);
    }

}
